package com.xiaomai.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaomai.commonutil.R;
import com.xiaomai.staservice.client.UcenterClient;
import com.xiaomai.staservice.entity.StatisticsDaily;
import com.xiaomai.staservice.mapper.StatisticsDailyMapper;
import com.xiaomai.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {


    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        R registersR = ucenterClient.countregister(day);
        Integer countRegister = (Integer) registersR.getData().get("countRegister");

        //将获取到的注册人数添加进数据库中
        StatisticsDaily sta = new StatisticsDaily();
        sta.setRegisterNum(countRegister);//添加注册人数数据
        sta.setDateCalculated(day);//添加注册日期
        sta.setLoginNum(RandomUtils.nextInt(100,200));//其它添加随机数
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
            baseMapper.insert(sta);//将对象添加进数据库
    }

    @Override
    public Map<String, Object> getChartData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
        dayQueryWrapper.select(type, "date_calculated");
        dayQueryWrapper.between("date_calculated", begin, end);

        List<StatisticsDaily> dayList = baseMapper.selectList(dayQueryWrapper);


        List<Integer> numDataList = new ArrayList<Integer>();
        List<String> date_calculatedList = new ArrayList<String>();



        for (int i = 0; i < dayList.size(); i++) {
            StatisticsDaily daily = dayList.get(i);
            date_calculatedList.add(daily.getDateCalculated());
            switch (type) {
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("numDataList",numDataList);
        map.put("date_calculatedList",date_calculatedList);
        return map;
    }
}
